"use strict";

import Vue from "vue";
import axios from "axios";
import { Message, Notification } from "element-ui";

// Full config:  https://github.com/axios/axios#request-config
// axios.defaults.baseURL = process.env.baseURL || process.env.apiUrl || '';
// axios.defaults.headers.common['Authorization'] = AUTH_TOKEN;
// axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';

let config = {
  // baseURL: process.env.baseURL || process.env.apiUrl || ""
  baseURL: "http://localhost:9090",
  timeout: 6000, // Timeout
  withCredentials: true, // Check cross-site Access-Control
};

const _axios = axios.create(config);

_axios.interceptors.request.use(
  function (config) {
    // Do something before request is sent
    //方便开发测试
    Message.success("将要发送请求内容： " + JSON.stringify(config.data));
    return config;
  },
  function (error) {

    return Promise.reject(error);
  }
);

// Add a response interceptor
_axios.interceptors.response.use(
  function (response) {
    // Do something with response data
    return response;
  },
  function (error) {
    // Do something with response error
    if (error && error.response) {
      switch (error.response.status) {
        case 701:
          //text = "今天是周六";
          Message.warning("异常：" + error.response.data);
          break;
        case 710:
          Message.error(error.response.data);
          //text = "今天是周六";
          break;
        case 711:
          Notification.error(error.response.data);
          //text = "今天是周日";
          break;
        case 719:
          Message.warning("未知登录异常" + error.response.data);
          //text = "今天是周日";
          break;
        case 720:
          Message.warning("用户管理模块异常： " + error.response.data);
          //text = "今天是周日";
          break;
        case 731:
          //text = "今天是周日";
          Notification.warning("通知：" + error.response.data);
          break;
        case 810:
          Notification.warning("请假业务异常：" + error.response.data);
          break;
        case 819:
          Notification.warning("请假业务异常：" + error.response.data);
          break;
        case 820:
          Notification.error("请假权限拦截：" + error.response.data);
          break;
        case 1024:
          //text = "今天是周日";
          Message.error("未知后端异常：" + error.response.data);
          break;
        default:
          console.log("后端异常：" + error.response.data);
        //text = "期待周末~";
      }
    }

    return Promise.reject(error);
  }
);

Plugin.install = function (Vue, options) {
  Vue.axios = _axios;
  window.axios = _axios;
  Object.defineProperties(Vue.prototype, {
    axios: {
      get() {
        return _axios;
      },
    },
    $axios: {
      get() {
        return _axios;
      },
    },
    $http: {
      get() {
        return _axios;
      },
    },
  });
};

Vue.use(Plugin);

export default Plugin;
